function init(){
	var page = 1;
	var rows = 10;
	//w调用日期选择插件
    $('#datePicker').daterangepicker();
    
	$('.top-block ul li').click(function(){
	    $(this).addClass('active').siblings('li').removeClass('active');
	});
	
	// 仓库名称搜索
    $('.search-box').on('click','span',function(){
    	var conditions = findSearchConditions();
    	loadComment(page,rows,conditions[0],conditions[1],conditions[2]);
    })
    
    // 发货时间查询的方法
	$('#datePicker').on('change',function(){
		var conditions = findSearchConditions();
		loadComment(page, rows, conditions[0], conditions[1], conditions[2]);
	});
    
    // 状态查询的方法
	$('#isRelease').on('click','li',function(){
		var conditions = findSearchConditions();
		loadComment(page, rows, conditions[0], conditions[1], conditions[2]);
	});
    
    loadComment(page, rows, '', '', '');
}

/**
 * 获取所有的查询条件
 */
function findSearchConditions(){
	var strs = new Array();
	strs[0] = $('#orderCode').val();
	strs[1] = $('#datePicker').val();
	var isRelease;
	var isReleases = $('#isRelease').children();
	isReleases.each(function(){
	    if($(this).hasClass('active')){
	    	isRelease = $(this).attr('data-id');
	    	return false;
	    }
	});
	strs[2] = isRelease;
	return strs;
}

/**
 * 加载数据
 * @param page
 * @param rows
 */
function loadComment(page, rows, orderCode, orderTime, status) {
	row = page;
	$.ajax({
		type: "post",
		url: '/diconet/orderlang/pay/findAccountsReceivableList',
		data: {
			page : page,
			rows : rows,
			orderCode: orderCode,
			orderTime: orderTime,
			status: status
		},
		dataType: 'json',
		success: function(data){
			var commentContent = '';
			if(data) {
				$('.total .money').text(monetaryFormat(0, data.orderAmount, 2));
				$('.yishou .money').text(monetaryFormat(0, data.payAmount, 2));
				$('.queren .money').text(monetaryFormat(0, data.confirmAmount, 2));
				$('.daishou .money').text(monetaryFormat(0, (data.orderAmount-data.payAmount-data.confirmAmount), 2));
				for(var i=0; i<data.rows.length; i++) {
					var comment = data.rows[i];
					var paymentState = comment.paymentState==1?'已结清':'付款中';
					commentContent += '<tr>';
					commentContent += '<td></td>';
					commentContent += '<td class="t1">'+comment.name+'</td>';
					commentContent += '<td class="t1">'+comment.orderCode+'</td>';
					commentContent += '<td class="t1">'+monetaryFormat(0, comment.orderAmount, 2)+'</td>';
					commentContent += '<td class="t1">'+monetaryFormat(0, comment.payAmount, 2)+'</td>';
					commentContent += '<td class="t1">'+monetaryFormat(0, comment.confirmAmount, 2)+'</td>';
					commentContent += '<td class="t1">'+monetaryFormat(0, (comment.orderAmount-comment.payAmount-comment.confirmAmount), 2)+'</td>';
					commentContent += '<td class="t1">'+paymentState+'</td>';
					commentContent += '</tr>';
				}
				$('.order-list-tbody').html(commentContent);
				var dataPage = data.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+');">尾页</a>';
				}
			}
			$(".quotes").html(pageContent);
		},
		error : function(data){
			layer.msg('请求失败', {time:1000,icon:5}); 
		}
	});
}

/**
 * 字符串转货币格式
 * @param type 货币类型 0 '￥' 1 '$'
 * @param s
 * @param n
 * @returns {String}
 */
function monetaryFormat(type, s, n){
	var moneyType = new Array('￥','$');
    n = n > 0 && n <= 20 ? n : 2;   
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";   
    var l = s.split(".")[0].split("").reverse(),   
    r = s.split(".")[1];   
    t = "";   
    for(i = 0; i < l.length; i ++ )   
    {   
       t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");   
    }   
    return moneyType[type] + t.split("").reverse().join("") + "." + r;   
 }

init();

